<?php
uses('sanitize');
class UsersController extends AppController {

	var $name = 'Users';
	var $uses = array('User');
	var $helpers = array('Html', 'Form', 'Text', 'Time' );
	
	function login() { 
		$this->pageTitle = 'Login';
		//$this->checkCookie();
		
		if ($this->data) { 
				$san = new Sanitize(); 
				$cleaned_username = $san->paranoid($this->data['User']['username']); 
				//$username = $this->data['User']['username'];
				$password = md5($this->data['User']['password']);
	
				$results = $this->User->findByUsername($cleaned_username); 
				if ($results && $results['User']['password'] == $password) { 
					
					// Write session
					$this->Session->write('user', $results['User']['username']); 
					$this->Session->write('last_login', $results['User']['last_login']); 
					// Write last login
					$results['User']['last_login'] = date("Y-m-d H:i:s"); 
					if( $this->data['User']['remember'] == 'on') {
						// Create a random string and save in in User and cookie
						$randomstring = $this->createRandomString(30);
						$results['User']['cookie'] = $randomstring;
						$value = array( md5($results['User']['username'] ), $randomstring);
						$this->cookie->write('login',$value,'+30 day');
					}
					$this->User->save($results); 
					$this->redirect('/admin/posts'); 
					
				} else { 
					$this->Session->setFlash('Invalid Login.');
				} 
				//$this->set('debug', $_COOKIE['glogin']);
			} 

		}
		
	function logout() { 
			$this->Session->delete('user'); 
			$this->cookie->delete('login');
			$this->redirect('/users/login'); 
	}
	
	function admin_index() {
		$this->pre_admin();
		$this->User->recursive = 0;
		$this->set('users', $this->User->findAll());
	}

	function admin_view($username = null) {
		$this->pre_admin();
		if($user = $this->User->findByUsername($username)) {
			$this->set('user', $user);
		} else {
			$this->Session->setFlash('No user with that username.');
			$this->redirect('/admin/users/index');
		}
	}

	function admin_addedit($id = null) {
		$this->pre_admin();
		if(empty($this->data)) {
			if($id) {
				$this->data = $this->User->read(null, $id);
			} else {
				// Create new user, no need to set anything default
			}
		} else {
			$this->cleanUpFields();
			$this->data['User']['password'] = md5($this->data['User']['password']);
			if($this->User->save($this->data)) {
				$this->Session->setFlash('The User has been saved');
				if(!$this->data['User']['id']) {
					$new_id = $this->User->getInsertID();
					$this->Session->setFlash('The new User has been saved!');
					$this->redirect('/admin/users/addedit/' . $new_id);
				}
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_delete($id = null) {
		$this->pre_admin();
		if(!$id) {
			$this->Session->setFlash('Invalid id for User');
			$this->redirect('/admin/users/index');
		}
		if($this->User->del($id)) {
			$this->Session->setFlash('The User deleted: id '.$id.'');
			$this->redirect('/admin/users/index');
		}
	}

}
?>